package com.lsh.dao;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.lsh.common.util.PageSurport;

@Repository
public class  BasicDao extends SqlSessionDaoSupport {
	
	/**插入数据
	 * @param insert
	 * @param object
	 * @return
	 */
	public int insert(String insert, Object object) {
		return (Integer) this.getSqlSession().insert(insert, object);
	}

	/**批量插入
	 * @param insertBatch
	 * @param object
	 * @return
	 */
	public int insertBatch(String insertBatch, Object object) {
		return (Integer) this.getSqlSession().insert(insertBatch, object);
	}
	
	/**删除
	 * @param delete
	 * @param object
	 * @return
	 */
	public int delete(String delete, Object object) {
		return this.getSqlSession().delete(delete, object);
	}
	
	/**更新
	 * @param key
	 * @param object
	 * @return
	 */
	public int update(String key, Object object) {
		return getSqlSession().update(key, object);
	}
	
	/**执行mapper中select语句，返回对象用于 getById
	 * @param <T>
	 * @param sqlKey
	 * @param params
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T selectOne(String sqlKey, Object params) {
		return (T) this.getSqlSession().selectOne(sqlKey, params);
	}
	
	public <T> List<T> selectList(String sqlKey, Object params) {
		return this.getSqlSession().selectList(sqlKey, params);
	}
	 
	/**分页
	 * @param getPage
	 * @param getPageCount
	 * @param page
	 * @param params
	 * @return
	 */
	@SuppressWarnings(value = "unchecked")
	public  PageSurport selectPageByPage(String getPage,String getPageCount,PageSurport page,Map params){
		params.put("pageSize", String.valueOf(page.getPageSize()));
		int count = selectOne(getPageCount,params);
		page.setRecordCount(count);
		params.put("pageIndex", String.valueOf(page.getPageNo()));
		List items = selectList(getPage,  params);
		page.setItems(items);
		return page;
	}
	/**获取全部记录
	 * @param <T>
	 * @param sqlKey
	 * @param orderBy
	 * @return
	 */
	public  <T>List<T> selectAll(String sqlKey,String orderBy){
		return selectList(sqlKey, orderBy);
	}
	/**获取主键ID获取对应记录
	 * @param <T>
	 * @param getById
	 * @param params
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getById(String getById, Object params) {
		return (T) this.getSqlSession().selectOne(getById, params);
	}
	
	/**获取记录数
	 * @param getPageCount
	 * @return
	 */
	public int getCount(String getPageCount){
		return this.getSqlSession().selectOne(getPageCount,new Object());
	}
	
	/**根据条件获取记录数
	 * @param getPageCount
	 * @param map 条件Map
	 * @return
	 */
	public int getPageCount(String getPageCount,@SuppressWarnings("rawtypes") Map map){
		return this.getSqlSession().selectOne(getPageCount,map);
	}
}
